package edu.ptu.shortlink.admin.config;

import edu.ptu.shortlink.admin.common.biz.user.UserFlowRiskControlFilter;
import edu.ptu.shortlink.admin.common.biz.user.UserTransmitFilter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @BelongsProject: shortlink
 * @BelongsPackage: edu.ptu.shortlink.admin.config
 * @Author: HYJ
 * @CreateTime: 2025-04-26  01:18
 * @Description: 用户配置自动装配
 * @Version: 1.0
 */
@Configuration
public class UserConfiguration {
    @Bean
    public FilterRegistrationBean<UserTransmitFilter> globalUserTransmitFilter(StringRedisTemplate redisTemplate) {

        FilterRegistrationBean<UserTransmitFilter> registration = new FilterRegistrationBean<>();

        registration.setFilter(new UserTransmitFilter());
        registration.addUrlPatterns("/*");
        registration.setOrder(0);

        return registration;

    }

    @Bean
    @ConditionalOnProperty(name = "short-link.flow-window.enable", havingValue = "true")
    public FilterRegistrationBean<UserFlowRiskControlFilter> globalUserFlowRiskControlFilter(
            StringRedisTemplate redisTemplate,
            UserFlowRiskControlConfiguration userFlowRiskControlConfiguration
    ) {
        FilterRegistrationBean<UserFlowRiskControlFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new UserFlowRiskControlFilter(
                redisTemplate, userFlowRiskControlConfiguration
        ));

        registration.addUrlPatterns("/*");
        registration.setOrder(10);

        return registration;
    }
}
